package com.code.entity.composite;

import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 树枝根节点
 */
public class PomComponent extends NodeComponent {
    //存储菜单或者菜单项
    private List<NodeComponent> menuComponentList = new ArrayList<>();

    protected String key;
    protected String desc;

    public PomComponent(String key, String desc) {
        this.key = key;
        this.desc = desc;
    }

    @Override
    public void add(NodeComponent nodeComponent) {
        menuComponentList.add(nodeComponent);
    }

    @Override
    public String build(String nbsp) {
        StringBuilder builder = new StringBuilder();
        builder.append(nbsp);
        builder.append("<").append(key);
        if (StringUtils.isNotBlank(desc)) {
            builder.append("  ").append(desc);
        }
        builder.append(">\n");
        for (NodeComponent nodeComponent : menuComponentList) {
            builder.append(nodeComponent.build(nbsp+"    "));
            builder.append("\n");
        }
        builder.append(nbsp);
        builder.append("</").append(key).append(">");
        return builder.toString();
    }
}
